const app = getApp()

Page({
  data: {
    currentIndex: 0,
    totalWords: 20, // 每组学习20个单词
    masteredCount: 0,
    isShowMeaning: false,
    isCollected: false,
    currentWord: {
      word: 'abandon',
      phonetic: 'əˈbændən',
      meaning: 'v. 放弃，抛弃；离弃',
      definition: 'to leave someone or something forever',
      example: 'He abandoned his car and continued on foot.',
      translation: '他抛弃了他的车，徒步继续前行。'
    },
    wordList: [], // 当前学习的单词列表
    masteredWords: [], // 已掌握的单词
    notRememberedWords: [], // 未掌握的单词
  },

  onLoad(options) {
    // 从数据库或缓存获取单词列表
    this.loadWordList()
  },

  // 加载单词列表
  loadWordList() {
    // TODO: 从数据库获取单词列表
    // 这里先用模拟数据
    const mockWordList = [
      {
        word: 'abandon',
        phonetic: 'əˈbændən',
        meaning: 'v. 放弃，抛弃；离弃',
        definition: 'to leave someone or something forever',
        example: 'He abandoned his car and continued on foot.',
        translation: '他抛弃了他的车，徒步继续前行。'
      },
      // ... 更多单词
    ]
    
    this.setData({
      wordList: mockWordList,
      currentWord: mockWordList[0]
    })
  },

  // 显示单词释义
  showMeaning() {
    this.setData({
      isShowMeaning: true
    })
  },

  // 播放单词音频
  playAudio() {
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.src = `https://dict.youdao.com/dictvoice?audio=${this.data.currentWord.word}&type=2`
    innerAudioContext.play()
  },

  // 处理"认识"按钮点击
  handleRemember() {
    const { currentIndex, totalWords, masteredCount, currentWord, masteredWords } = this.data
    
    // 添加到已掌握列表
    masteredWords.push(currentWord)
    
    this.setData({
      masteredCount: masteredCount + 1
    })
    
    this.nextWord()
  },

  // 处理"不认识"按钮点击
  handleNotRemember() {
    const { currentWord, notRememberedWords } = this.data
    
    // 添加到未掌握列表
    notRememberedWords.push(currentWord)
    
    this.nextWord()
  },

  // 下一个单词
  nextWord() {
    const { currentIndex, totalWords, wordList } = this.data
    
    if (currentIndex < totalWords - 1) {
      this.setData({
        currentIndex: currentIndex + 1,
        currentWord: wordList[currentIndex + 1],
        isShowMeaning: false,
        isCollected: false
      })
    } else {
      // 完成本组单词学习
      wx.showModal({
        title: '学习完成',
        content: '本组单词已学习完成，是否查看学习报告？',
        success: (res) => {
          if (res.confirm) {
            this.showReport()
          } else {
            wx.navigateBack()
          }
        }
      })
    }
  },

  // 返回上一个单词
  handlePrevious() {
    const { currentIndex, wordList } = this.data
    
    if (currentIndex > 0) {
      this.setData({
        currentIndex: currentIndex - 1,
        currentWord: wordList[currentIndex - 1],
        isShowMeaning: false,
        isCollected: false
      })
    }
  },

  // 收藏单词
  handleCollect() {
    const { isCollected, currentWord } = this.data
    
    this.setData({
      isCollected: !isCollected
    })
    
    if (!isCollected) {
      // TODO: 保存到收藏列表
      wx.showToast({
        title: '已收藏',
        icon: 'success'
      })
    } else {
      // TODO: 从收藏列表移除
      wx.showToast({
        title: '已取消收藏',
        icon: 'none'
      })
    }
  },

  // 添加笔记
  handleNote() {
    // TODO: 实现笔记功能
    wx.showToast({
      title: '笔记功能开发中',
      icon: 'none'
    })
  },

  // 显示学习报告
  showReport() {
    // TODO: 跳转到学习报告页面
    wx.showToast({
      title: '学习报告开发中',
      icon: 'none'
    })
  }
}) 